import scrapy
import json
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
from scrapy.exceptions import NotConfigured
from scrapy.pipelines.images import ImagesPipeline

from doubanbook.mylib.mypymysql import mypymysql

class itcastItemPipeline(object):

    def __init__(self, file_name=None):
        self.file_name = 'itcast.json'
        self.fp = None
        self.mypy = mypymysql()

    def open_spider(self, spider):
        self.fp = open(self.file_name, 'w', encoding='utf-8')

    def close_spider(self, spider):
        self.fp.close()

    def process_item(self, item, spider):
        json_item = json.dumps(dict(item))
        self.fp.write(json_item + "\n")
        # self.mypy.mydateadd(page=item['page'], name=item['name'], img=item['img'], price=item['price'], url=item['url'],
        #                     shop=item['shop'], commit=item['commit'])
        return item
